package com.deer.wms.review.manage.web;

import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.review.manage.model.ReviewNode;
import com.deer.wms.review.manage.model.ReviewNodeCriteria;
import com.deer.wms.review.manage.service.ReviewNodeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.deer.wms.review.manage.model.ReviewNodeDto;
import springfox.documentation.annotations.ApiIgnore;


/**
* Created by guo on 2018/10/24.
*/
@Api(description = "审核节点信息api接口")
@RestController
@RequestMapping("/review/nodes")
public class ReviewNodeController {

    @Autowired
    private ReviewNodeService reviewNodeService;

    @PostMapping("/add")
    public Result add(@RequestBody ReviewNode reviewNode, @ApiIgnore @User CurrentUser currentUser) {
        reviewNodeService.save(reviewNode);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete( Integer reviewNodeId) {

        if(reviewNodeService.checkExistBill(reviewNodeId)){
            ReviewNode reviewNode = reviewNodeService.getById(reviewNodeId);

            Integer reviewMasterId =reviewNode.getReviewMasterId();
            reviewNodeService.removeById(reviewMasterId);
            return ResultGenerator.genSuccessResult();
        }else
        {
            return ResultGenerator.genFailResult(CommonCode.CANT_DELETE_REVIEW_NODE,"该节点有正在审核的单据，不可删除！",null);
        }

    }

    @PostMapping("/update")
    public Result update(@RequestBody ReviewNode reviewNode) {
        reviewNodeService.updateById(reviewNode);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        ReviewNode reviewNode = reviewNodeService.getById(id);
        return ResultGenerator.genSuccessResult(reviewNode);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType="header", dataType="String", required = true)
    })
    @GetMapping("/list")
    public Result list(ReviewNodeCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<ReviewNodeDto> list = reviewNodeService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
